<?php
/*******************************************************************************
 * $Date:: Jan 7, 2011 9:46:01 PM                                              $
 * $Revision:                                                                  $
 * $Author:: Tuan Duong                                                        $
 * $Comment::                                                                  $
 ******************************************************************************/ 
/**
 * Description here...
 * 
 * @package Lua
 * @subpackage Controller
 * @author Tuan Duong <bacduong[at]gmail[dot]com>
 * @version 
 */

class Lua_Controller
{
    /**
     * @var Lua_View_Abstract
     */
    protected $view;

    protected $params = array();
    
    /**
     * @var Lua_Request
     */
    protected $request;

    public function __construct()
    {
        $this->view = new Lua_View();
        $this->request = Lua_Request::getInstance();
        $this->init();
    }

    protected function init()
    {
        $this->view->assign('loggedUser', $this->getLoggedUser());
    }

    public function render()
    {
        echo $this->view->render();
    }

    protected function forward(array $params)
    {
        $router = Lua_Registry::get(Lua_Constant_General::LUA_ROUTER);
        if (isset($params[Lua_Constant_General::PARAM_MODULE])) {
            $router->setModule($params[Lua_Constant_General::PARAM_MODULE]);
        }

        if (isset($params[Lua_Constant_General::PARAM_CONTROLLER])) {
            $router->setController($params[Lua_Constant_General::PARAM_CONTROLLER]);
        }

        if (isset($params[Lua_Constant_General::PARAM_ACTION])) {
            $router->setAction($params[Lua_Constant_General::PARAM_ACTION]);
        }

        $dispatcher = Lua_Registry::get(Lua_Constant_General::LUA_DISPATCHER);
        /* @var $dispatcher Lua_Dispatcher_Interface */
        $controller = $dispatcher->getControllerName($router);
        $controller->setView($this->getView());
        $action = $dispatcher->getActionName($router);
        // Re-initilize adapter for view
        return $controller->{$action}();
    }

    protected function redirect(array $params = array())
    {
        $router = Lua_Registry::get(Lua_Constant_General::LUA_ROUTER);
        /* @var $router Lua_Router */
        header('Location: ' . $router->url($params));
        exit();
    }

    /**
     * @return Lua_Bean_IpooUser
     */
    protected function getLoggedUser()
    {
        return Lua_Util_Session::getLoggedUser();        
    }
    
    protected function isLogged($role = 'user')
    {
        $loggedUser = $this->getLoggedUser();
        return !is_null($loggedUser->id);
    }

    public function setParam($param, $value)
    {
        $this->params[$param] = $value;
    }

    public function getParam($param)
    {
        if (isset($this->params[$param])) {
            return $this->params[$param];
        } else {
            return null;
        }
    }

    public function setView(Lua_View_Abstract $view)
    {
        $this->view = $view;
    }

    public function getView()
    {
        return $this->view;
    }

    protected function initAcl()
    {
        $acl = Lua_Acl::getInstance();
        if (!$acl->canAccess()) {
            $this->forward(array(
                Lua_Constant_General::PARAM_CONTROLLER => 'error',
                Lua_Constant_General::PARAM_ACTION => 'no-authorized'
            ));
        }
    }

    public function preDispatch()
    {
    }

    public function postDispatch()
    {

    }
}